home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / drawst_1 / frmdraws.frm (.txt) next >
Encoding:
Visual Basic Form  |  1998-10-12  |  8.6 KB  |  174 lines

  1. VERSION 5.00
  2. Begin VB.Form frmDrawState 
  3.    AutoRedraw      =   -1  'True
  4.    Caption         =   "DrawState Demo"
  5.    ClientHeight    =   2760
  6.    ClientLeft      =   1395
  7.    ClientTop       =   1530
  8.    ClientWidth     =   4965
  9.    LinkTopic       =   "Form1"
  10.    ScaleHeight     =   184
  11.    ScaleMode       =   3  'Pixel
  12.    ScaleWidth      =   331
  13.    Begin VB.CommandButton cmdState 
  14.       Caption         =   "Draw Disabled"
  15.       Height          =   360
  16.       Index           =   1
  17.       Left            =   2655
  18.       TabIndex        =   1
  19.       Top             =   2160
  20.       Width           =   1455
  21.    End
  22.    Begin VB.CommandButton cmdState 
  23.       Caption         =   "Draw Normal"
  24.       Height          =   360
  25.       Index           =   0
  26.       Left            =   855
  27.       TabIndex        =   0
  28.       Top             =   2160
  29.       Width           =   1455
  30.    End
  31. Attribute VB_Name = "frmDrawState"
  32. Attribute VB_GlobalNameSpace = False
  33. Attribute VB_Creatable = False
  34. Attribute VB_PredeclaredId = True
  35. Attribute VB_Exposed = False
  36.   ' demo project showing how to use the DrawState API function
  37.   ' by Bryan Stafford of New Vision Software
  38.  - newvision@imt.net
  39.   ' this demo is released into the public domain "as is" without
  40.   ' warranty or guaranty of any kind.  In other words, use at
  41.   ' your own risk.
  42.   ' IMPORTANT NOTE: this demo will NOT display the icons in the
  43.   '                 VB5 IDE.  This is because the icons are stored
  44.   '                 in a resource file and windows does not know where
  45.   '                 to find the resources when LoadImage is called from
  46.   '                 the IDE.  To see the icons you must compile the
  47.   '                 project and run the exe.
  48.   Option Explicit
  49.   ' constant for the long value zero.  also used for NULL pointers.
  50.   Private Const API_FALSE As Long = 0&
  51.   ' DrawState constants
  52.   Private Const DSS_DISABLED As Long = &H20&
  53.   Private Const DSS_MONO As Long = &H80&
  54.   Private Const DSS_NORMAL As Long = &H0&
  55.   Private Const DSS_UNION As Long = &H10&
  56.   Private Const DST_BITMAP As Long = &H4&
  57.   Private Const DST_COMPLEX As Long = &H0&
  58.   Private Const DST_ICON As Long = &H3&
  59.   Private Const DST_PREFIXTEXT As Long = &H2&
  60.   Private Const DST_TEXT As Long = &H1&
  61.   ' alias used to draw graphics, notice the lData parameter
  62.   Private Declare Function DrawState& Lib "user32" Alias "DrawStateA" (ByVal hDC&, ByVal hBrush&, _
  63.       ByVal lpDrawStateProc&, ByVal lData&, ByVal wData&, ByVal X&, ByVal Y&, ByVal cx&, _
  64.                                                                               ByVal cy&, ByVal fFlags&)
  65.   ' alias used to draw text, notice the lData parameter
  66.   Private Declare Function DrawStateText& Lib "user32" Alias "DrawStateA" (ByVal hDC&, ByVal hBrush&, _
  67.       ByVal lpDrawStateProc&, ByVal lData$, ByVal wData&, ByVal X&, ByVal Y&, ByVal cx&, _
  68.                                                                               ByVal cy&, ByVal fFlags&)
  69.   ' LoadImage constants
  70.   Private Const IMAGE_BITMAP  As Long = 0& '<- loads a bitmap
  71.   Private Const IMAGE_ICON As Long = 1&    '<- loads an icon
  72.   Private Const IMAGE_CURSOR As Long = 2&  '<- loads a cursor
  73.   Private Const LR_DEFAULTCOLOR As Long = &H0& '<- default value
  74.   Private Const LR_SHARED As Long = &H8000& '<- use this in the fuLoad param to share the image handle
  75.   Private Const LR_LOADFROMFILE = &H10 '<- use this in the fuLoad param to load a graphic
  76.   ' from a file.  you will have to change lpszName to a string param to pass the file name
  77.   ' to the function.
  78.   ' alias used to load from resource ID number instead of string
  79.   Private Declare Function LoadImageBynum& Lib "user32" Alias "LoadImageA" (ByVal hInst&, ByVal lpszName&, _
  80.                                           ByVal uType&, ByVal cxDesired&, ByVal cyDesired&, ByVal fuLoad&)
  81.   ' used to destroy icon handles when we've finished with them
  82.   Private Declare Function DestroyIcon& Lib "user32" (ByVal hIcon&)
  83.   ' used to determine whether or not this demo is running in the IDE
  84.   Private Declare Function GetClassName& Lib "user32" Alias "GetClassNameA" (ByVal hWnd&, _
  85.                                                                 ByVal lpClassName$, ByVal nMaxCount&)
  86. Private Sub Form_Load()
  87.   ' center this form
  88.   Move (Screen.Width \ 2) - (Width \ 2), (Screen.Height \ 2) - (Height \ 2)
  89.   ' call the click event of the comand button to set the initial display
  90.   cmdState_Click False
  91.   Show
  92.   ' check to see of we are running in the IDE
  93.   If RunningInIde Then _
  94.           MsgBox "You will not see any icons when this demo is run in the IDE." & vbCrLf & _
  95.           vbCrLf & "See the ""IMPORTANT NOTE"" in the General Declarations" & vbCrLf & _
  96.           "section of the form code file for more info.", vbExclamation, "DrawState Demo"
  97. End Sub
  98. Private Sub cmdState_Click(Index As Integer)
  99.   ' the common sub for the command button array.  Index zero is the
  100.   ' "Draw Normal" button and index 1 is the "Draw Disabled" button
  101.   ' each of the icons used contains both a large (32x32) and
  102.   ' small (16x16) icon.  We will load and display both of the icons
  103.   ' from each file
  104.   ' set the icon sizes
  105.   Const ICONSIZELARGE As Long = 32&
  106.   Const ICONSIZESMALL As Long = 16&
  107.   Dim i&, hIcon&, fEnabledState&, nLeftPos&, nTopPos&, sTemp$
  108.   ' if this is the disabled command button, set the disabled flag used
  109.   ' in the call to the DrawState function
  110.   If Index = 1 Then fEnabledState = DSS_DISABLED
  111.   ' clear the form
  112.   Cls
  113.   ' ####  Draw the icons first  ####
  114.   ' set the position for the first drawing operation.
  115.   ' (232 is the width of 6 large icons and the spaces between them)
  116.   nLeftPos = (ScaleWidth \ 2) - (232 \ 2)
  117.   nTopPos = 10
  118.   ' loop through the icons in the resource file and draw both the large and small version
  119.   ' 101 is the first resource ID and 106 is the last resource ID
  120.   For i = 101 To 106
  121.     ' load the large icon from the resource file
  122.     hIcon = LoadImageBynum(App.hInstance, i, IMAGE_ICON, _
  123.                               ICONSIZELARGE, ICONSIZELARGE, LR_DEFAULTCOLOR)
  124.     ' draw the icon at the position indicated by nLeftPos, nTopPos
  125.     Call DrawState(hDC, API_FALSE, API_FALSE, hIcon, API_FALSE, nLeftPos, _
  126.                     nTopPos, API_FALSE, API_FALSE, DST_ICON Or fEnabledState)
  127.     ' destroy the icon
  128.     Call DestroyIcon(hIcon)
  129.     ' load the small icon from the resource file
  130.     hIcon = LoadImageBynum(App.hInstance, i, IMAGE_ICON, _
  131.                               ICONSIZESMALL, ICONSIZESMALL, LR_DEFAULTCOLOR)
  132.     ' set up the next drawing position coordinates
  133.     nLeftPos = nLeftPos + 8
  134.     nTopPos = nTopPos + 40
  135.     ' draw the icon at the position indicated by nLeftPos, nTopPos
  136.     Call DrawState(hDC, API_FALSE, API_FALSE, hIcon, API_FALSE, nLeftPos, _
  137.                     nTopPos, API_FALSE, API_FALSE, DST_ICON Or fEnabledState)
  138.     ' destroy the icon
  139.     Call DestroyIcon(hIcon)
  140.     ' set up the next drawing position coordinates
  141.     nLeftPos = nLeftPos + 32
  142.     nTopPos = nTopPos - 40
  143.   Next
  144.   ' ########  End icon code  ##################
  145.   '#####  Draw the text strings  ##############
  146.   ' assign tha string to the variable
  147.   sTemp = "A string without underlined mnemonics."
  148.   ' set the position for the text
  149.   nLeftPos = (ScaleWidth \ 2) - (TextWidth(sTemp) \ 2)
  150.   nTopPos = nTopPos + 70
  151.   ' call the text alias of DrawState to draw the text on the form
  152.   Call DrawStateText(hDC, API_FALSE, API_FALSE, sTemp, Len(sTemp), nLeftPos, _
  153.       nTopPos, API_FALSE, API_FALSE, DST_TEXT Or fEnabledState)
  154.   ' repete the operation for the string with underlined mnemonic
  155.   sTemp = "&A string with an underlined mnemonic."
  156.                                           ' account for the '&' character
  157.   nLeftPos = (ScaleWidth \ 2) - (TextWidth(Right$(sTemp, Len(sTemp) - 1)) \ 2)
  158.   nTopPos = nTopPos + (TextHeight(sTemp) * 2)
  159.   Call DrawStateText(hDC, API_FALSE, API_FALSE, sTemp, Len(sTemp), nLeftPos, _
  160.       nTopPos, API_FALSE, API_FALSE, DST_PREFIXTEXT Or fEnabledState)
  161.   ' ########  End text code  ##################
  162.   ' since this stuff was drawn on the AutoRedra memory
  163.   ' DC, referesh the form to get it to appear
  164.   Refresh
  165. End Sub
  166. Private Function RunningInIde() As Boolean
  167.   Dim sClassName$, nStrLen&
  168.   ' check to see where we're running in the IDE
  169.   sClassName = String$(260, vbNullChar)
  170.   nStrLen = GetClassName(hWnd, sClassName, Len(sClassName))
  171.   If nStrLen Then sClassName = Left$(sClassName, nStrLen)
  172.   If sClassName = "ThunderForm" Then RunningInIde = True
  173. End Function
  174.